package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.regexp.internal.RE;
import com.tanhua.domain.mongo.*;
import com.tanhua.dubbo.mapper.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class TestSoulApilmpl implements TestSoulApi {

    @Autowired
    private TestSoulMapper testSoulMapper;
    @Autowired
    private TestOptionMapper testOptionMapper;
    @Autowired
    private TestQuestionSoulMapper testQuestionSoulMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private ResultMapper resultMapper;

    @Override
    public List<TestSoul> testBysidSoullist() {
        List<TestSoul> testSouls = testSoulMapper.selectList(null);
        return testSouls;
    }

    @Override
    public List<Option> testOptionlist(Long id) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", id);
        List<Option> options = testOptionMapper.selectList(queryWrapper);
        return options;
    }
    //查询列表
    @Override
    public List<QuestionSoul> testQuestions(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid", id);
        List<QuestionSoul> questionSouls = testQuestionSoulMapper.selectList(queryWrapper);
        return questionSouls;
    }

    @Override
    public Option findByoptionid(String optionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", optionId);
        Option option = testOptionMapper.selectOne(queryWrapper);
        return option;
    }

    @Override
    public Long findByquestionid(String s) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", s);
        QuestionSoul questionSoul = testQuestionSoulMapper.selectOne(queryWrapper);
        return Long.valueOf(questionSoul.getRid());
    }

    @Override
    public void insterReport(Report report) {
        reportMapper.insert(report);

    }

    //根据报告id查找分数
    @Override
    public Integer findByreportid(String reportId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid", reportId);
        Report report = reportMapper.selectOne(queryWrapper);
        return report.getScore();
    }

    //根据分数查找
    @Override
    public Result selectByScore(Integer score) {
        Result result = resultMapper.selectScore(score);
        return result;
    }

    //更改字段
    @Override
    public void updateIslock(String lock) {
        UpdateWrapper<TestSoul> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("level", lock);
        queryWrapper.set("is_Lock", 0);
        testSoulMapper.update(null, queryWrapper);
    }

    //查找报告id
    @Override
    public Long testByreportid(String reportid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid", reportid);
        Report report = reportMapper.selectOne(queryWrapper);
        return report.getSid();
    }

    //根据id查找报告
    @Override
    public Report findByuid(Long userId, Long soulid) {
        Report report = reportMapper.selectByuserIdOne(userId, soulid);
        return report;
    }

    //修改报告信息
    @Override
    public void updatereport(Long id, Report report) {
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("id", id);
        reportMapper.update(report, queryWrapper1);
    }
}
